package com.multiplayer.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;




import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.multiplayer.entity.StudyRoom;
import com.multiplayer.service.IStudyRoomService;
import com.multiplayer.service.impl.StudyRoomServiceImpl;
import com.multiplayer.tools.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.groovy.GJson;




/**
 * Servlet implementation class MovieManageServlet
 */
@WebServlet("/StudyRoomManageServlet")
public class StudyRoomManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 IStudyRoomService studyRoomService=new StudyRoomServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudyRoomManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 1设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setContentType("application/json");
				
				// 标记当前为何种操作的变量
				String op = "";
				if (request.getParameter("op") != null) {
					op = request.getParameter("op");
				}
				System.out.println("op:" + op);
		        if ("queryByPage".equals(op)) {
					// 分页显示数据
					doQueryByPage(request, response);
				}
		        else if ("editStudyRoom".equals(op)) {
					// 修改自习室信息
					try {
						doEditStudyRoom(request, response);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		        else if ("deleteStudyRoom".equals(op)) {
					// 删除自习室
					doDeleteStudyRoom(request, response);
				}
		        else if ("addStudyRoom".equals(op)) {
					// 分页有关的查询 数据处理 页面跳转等
					try {
						doAddStudyRoom(request, response);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
			/**
			 * 增加自习室
			 * @param request
			 * @param response
			 * @throws ParseException 
			 * @throws IOException 
			 * @throws ServletException 
			 */
	
	private void doAddStudyRoom(HttpServletRequest request, HttpServletResponse response) throws ParseException, ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("执行doAddStudyRoom");
		String Rno=request.getParameter("studyRoomNo");
		String Ano=request.getParameter("studyRoomAdmin");
		String StartTime="";
		String EndTime="";
		String roomName=request.getParameter("roomName");
		String Rstatus=request.getParameter("studyRoomStatus");
		int seatNum=0;
		int freeSeatNum=0;
		
		SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date startDate=null;
		Date endDate=null;
		
		if(request.getParameter("seatNum")!=null)
		{
			seatNum=Integer.parseInt(request.getParameter("seatNum"));
		}
		if(request.getParameter("freeSeatNum")!=null)
		{
			freeSeatNum=Integer.parseInt(request.getParameter("freeSeatNum"));
		}
		
		if(!request.getParameter("StartTime").equals(""))
		{
			StartTime=request.getParameter("StartTime");
			startDate=s.parse(StartTime);
		}
		if(!request.getParameter("EndTime").equals(""))
		{
			EndTime=request.getParameter("EndTime");
			endDate=s.parse(EndTime);
		}
		
		StudyRoom studyRoom=new StudyRoom(Rno,Ano,roomName,seatNum,Rstatus,freeSeatNum,startDate,endDate);
		System.out.println("添加studyRoom："+studyRoom);
		if(studyRoomService.addStudyRoom(studyRoom))
		{
			System.out.println("添加成功");
		}
		else
		{
			System.out.println("添加失败");
		}
		doQueryByPage(request, response);//刷新数据到页面
	}

	/**
	 * 删除自习室
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
			private void doDeleteStudyRoom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				System.out.println("执行doDeleteStudyRoom");
				String roomNo=request.getParameter("Rno");
				if(studyRoomService.deleteStudyRoomById(roomNo))
				{
					System.out.println("删除成功");
				}
				else
				{
					System.out.println("删除失败");
				}
				
				doQueryByPage(request, response);//刷新数据到页面
	}

			/**
			 * 修改自习室
			 * @param request
			 * @param response
			 * @throws ServletException
			 * @throws IOException
			 * @throws ParseException 
			 */
			private void doEditStudyRoom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
		// TODO Auto-generated method stub
				System.out.println("执行doEditStudyRoom");
				String Rno=request.getParameter("studyRoomNo");
				String Ano=request.getParameter("studyRoomAdmin");
				String StartTime="";
				String EndTime="";
				String roomName=request.getParameter("roomName");
				String Rstatus=request.getParameter("studyRoomStatus");
				int seatNum=0;
				int freeSeatNum=0;
				
				SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date startDate=null;
				Date endDate=null;
		
				if(request.getParameter("seatNum")!=null)
				{
					seatNum=Integer.parseInt(request.getParameter("seatNum"));
				}
				if(request.getParameter("freeSeatNum")!=null)
				{
					freeSeatNum=Integer.parseInt(request.getParameter("freeSeatNum"));
				}
				
				if(!request.getParameter("StartTime").equals(""))
				{
					StartTime=request.getParameter("StartTime");
					startDate=s.parse(StartTime);
				}
				if(!request.getParameter("EndTime").equals(""))
				{
					EndTime=request.getParameter("EndTime");
					endDate=s.parse(EndTime);
				}
				StudyRoom studyRoom=new StudyRoom(Rno,Ano,roomName,seatNum,Rstatus,freeSeatNum,startDate,endDate);
				System.out.println("修改studyRoom："+studyRoom);
				if(studyRoomService.editStudyRoom(studyRoom))
				{
					System.out.println("修改成功");
				}
				else
				{
					System.out.println("修改失败");
				}
				doQueryByPage(request, response);//刷新数据到页面
	}

			/**
			 * 模糊查询 分页
			 * 
			 * @param request
			 * @param response
			 * @throws ServletException
			 * @throws IOException
			 */
			private void doQueryByPage(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
				System.out.println("执行doQueryByPage");
				String keywords = "";
				// 获取请求参数
				if (request.getParameter("keywords") != null) {
					keywords = request.getParameter("keywords");
				}
				// 页码默认值为1
				int page = 1;
				// 每页记录数默认为10
				int pageSize = 10;
				if (request.getParameter("page") != null) {
					page = Integer.parseInt(request.getParameter("page"));
				}

				if (request.getParameter("pageSize") != null) {
					pageSize = Integer.parseInt(request.getParameter("pageSize"));
				}

				// 3 调用 Service的方法
				PageData<StudyRoom> pd = studyRoomService.getStudyRoomByPage(page, pageSize, keywords);
				JSONObject pageInfoObj = new JSONObject();
				pageInfoObj.put("page",pd.getPage());
				pageInfoObj.put("totalCount", pd.getTotalCount());
				pageInfoObj.put("pageSize", pd.getPageSize());
				pageInfoObj.put("totalPage", pd.getTotalPage());
				JSONArray  DataArray = new JSONArray();
				JSONArray  array = new JSONArray();
				

				
				for(StudyRoom studyRoom: pd.getData()){
					String Rstime="";
					String Retime="";
					if(studyRoom.getRstime()!=null&&studyRoom.getRetime()!=null)
					{
				        Date date1 = studyRoom.getRstime();
				        Date date2 = studyRoom.getRetime();
				         //获得SimpleDateFormat类
					    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					        //打印当前时间
					    Rstime=sf.format(date1);
					    Retime=sf.format(date2);
					}
					
					JSONObject obj = new JSONObject();
					obj.put("Rno", studyRoom.getRno());
					obj.put("Ano", studyRoom.getAno());
					obj.put("Rposition",studyRoom.getRposition());
					obj.put("Rseatnum",studyRoom.getRseatnum());
					obj.put("Rstatus",studyRoom.getRstatus());
					obj.put("Rfreeseat",studyRoom.getRfreeseat());
					obj.put("Rstime",Rstime);
					obj.put("Retime",Retime);
					array.add(obj);
				}
				DataArray.add(array);
				DataArray.add(pageInfoObj);
				PrintWriter out=response.getWriter();
				out.println(DataArray);//传json数据给前端
			}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
